package com.micro.turtle.runner;

import cn.hutool.core.util.StrUtil;
import com.micro.turtle.constant.Cmd;
import com.micro.turtle.model.Conf;
import com.micro.turtle.util.Log;
import java.util.Map;
import java.util.Scanner;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 执行器
 *
 * @author jason
 */
public abstract class Runner {

    private static volatile Conf CONF;

    private final static Map<Cmd, Runner> CMD_RUNNERS = new ConcurrentHashMap<>();
    /**
     * 获取控制台打印内容
     */
    private static final Scanner SCANNER = new Scanner(System.in);

    protected Runner(Cmd cmd) {
        CMD_RUNNERS.put(cmd, this);
    }

    public static String scannerNext(String message) {
        System.out.println(message);
        return nextline();
    }

    public static String nextline() {
        String nextLine = SCANNER.nextLine();
        if (StrUtil.isBlank(nextLine)) {
            // 如果输入空行继续等待
            return SCANNER.next();
        }
        return nextLine;
    }

    public static void run() {
        run(Cmd.get(nextline().toLowerCase()));
    }


    public static void run(Cmd cmd) {
        CMD_RUNNERS.get(cmd).exec();
    }

    /**
     * 执行
     */
    abstract void exec();
}
